package com.ruoyi.Papers.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.DegreePoints.mapper.DegreePointsMapper;
import com.ruoyi.Papers.mapper.PapersMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.Papers.domain.Papers;
import com.ruoyi.Papers.service.IPapersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 论文表Controller
 * 
 * @author ruoyi
 * @date 2025-04-13
 */
@RestController
@RequestMapping("/Papers/Papers")
public class PapersController extends BaseController
{
    @Autowired
    private IPapersService papersService;

    @Autowired
    private PapersMapper papersMapper;

    @Autowired
    private DegreePointsMapper degreePointsMapper;

    /**
     * 查询论文表列表
     */
    @PreAuthorize("@ss.hasPermi('Papers:Papers:list')")
    @GetMapping("/list")
    public TableDataInfo list(Papers papers)
    {
        startPage();
        papers.setPointId(getLoginUser().getUsername().equals("admin")? 0 : getLoginUser().getUserId());
        List<Papers> list = papersService.selectPapersList(papers);
//        if (list.size() != 0 && getLoginUser().getUser().getRemark().equals("管理员")){
//            for (int i = 0; i < list.size(); i++) {
//                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());
//            }
//        }

        return getDataTable(list);
    }

    //查询专家评审论文信息
    @GetMapping("/byExpert")
    public TableDataInfo listByExpert()
    {
        startPage();
        List<Papers> list = papersMapper.selectByExpert(getLoginUser().getUserId());
        if(list.size() != 0 ){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setPointName(degreePointsMapper.selectDegreePointsByPointId(list.get(i).getPointId()).getPointName());
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出论文表列表
     */
    @PreAuthorize("@ss.hasPermi('Papers:Papers:export')")
    @Log(title = "论文表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Papers papers)
    {
        List<Papers> list = papersService.selectPapersList(papers);
        ExcelUtil<Papers> util = new ExcelUtil<Papers>(Papers.class);
        util.exportExcel(response, list, "论文表数据");
    }

    /**
     * 获取论文表详细信息
     */
    @PreAuthorize("@ss.hasPermi('Papers:Papers:query')")
    @GetMapping(value = "/{paperId}")
    public AjaxResult getInfo(@PathVariable("paperId") Long paperId)
    {
        return success(papersService.selectPapersByPaperId(paperId));
    }

    /**
     * 新增论文表
     */
    @PreAuthorize("@ss.hasPermi('Papers:Papers:add')")
    @Log(title = "论文表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Papers papers)
    {
        papers.setPointId(getLoginUser().getUserId());
        return toAjax(papersService.insertPapers(papers));
    }

    /**
     * 修改论文表
     */
    @PreAuthorize("@ss.hasPermi('Papers:Papers:edit')")
    @Log(title = "论文表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Papers papers)
    {
        return toAjax(papersService.updatePapers(papers));
    }

    /**
     * 删除论文表
     */
    @PreAuthorize("@ss.hasPermi('Papers:Papers:remove')")
    @Log(title = "论文表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{paperIds}")
    public AjaxResult remove(@PathVariable Long[] paperIds)
    {
        return toAjax(papersService.deletePapersByPaperIds(paperIds));
    }
}
